//
//  AY_DetailRecommendCell.m
//  AiYue
//
//  Created by boWen on 2023/11/14.
//

#import "AY_DetailRecommendCell.h"
#import "AY_DetailTJCell.h"
#import "AY_BookDetailViewController.h"
@interface AY_DetailRecommendCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UIView * bg_View;//底部圆角

@property (nonatomic, strong) UICollectionView * myCollectionView;
/** 标题  */
@property (strong,nonatomic) UILabel * topTitle;

@property (nonatomic, strong) NSArray * sourceAry;//数据源
@property (nonatomic, strong) UIButton * changeBtn;//换一换
@property (nonatomic, strong) UIImageView * changeImage;//换一换
@end

@implementation AY_DetailRecommendCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self addAllSubviewsWithUI];
    }
    return self;
}


- (void)addAllSubviewsWithUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.backgroundColor = COLOR(@"#F6F7FB");
    
    self.bg_View = [AYTool createViewWithFrame:CGRectZero andBg:[UIColor whiteColor] andRadius:YNUM(13) andAddSubView:self.contentView];
    [self.bg_View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.contentView).offset(YNUM(12));
        make.right.equalTo(self.contentView).offset(-YNUM(12));
        make.bottom.equalTo(self.contentView).offset(-YNUM(15));
    }];
    
    self.topTitle = [AYTool createLabelWithFrame:CGRectZero andText:@"为您推荐" andTextColor:COLOR(@"#000002") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(18)] andLines:1 andRadius:0 andAddSubView:self.bg_View];
    [self.topTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bg_View).offset(YNUM(15));
        make.left.equalTo(self.bg_View).offset(YNUM(12));
        make.height.mas_equalTo(YNUM(26));
    }];
    
    self.changeImage = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_change") andBg:COLOR_Bg andRadius:YNUM(4) andAddSubView:self.bg_View];
    self.changeImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(setChangeBookListAction2)];
    [self.changeImage addGestureRecognizer:tap];
    [self.changeImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bg_View).offset(YNUM(23.5));
        make.right.equalTo(self.bg_View).offset(YNUM(-12));
        make.height.mas_equalTo(YNUM(13));
        make.width.mas_equalTo(YNUM(13));
    }];
    self.changeBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"换一换" andFont:YFont(14) andTitleColor:COLOR(@"#97A4B0") andBg:nil andImage:nil andBgImage:nil andTarget:self andAction:@selector(setChangeBookListAction2) andRadius:0 andAddSubView:self.bg_View];
    [self.changeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bg_View).offset(YNUM(18));
        make.right.equalTo(self.changeImage.mas_left).offset(YNUM(-3));
        make.height.mas_equalTo(YNUM(26));
    }];
    [self.bg_View addSubview:self.myCollectionView];
    [self.myCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topTitle.mas_bottom).offset(YNUM(15));
        make.left.equalTo(self.topTitle.mas_left);
        make.right.equalTo(self.bg_View.mas_right).offset(YNUM(-12));
        make.bottom.equalTo(self.bg_View);
    }];
    
    UIView *line = [AYTool createViewWithFrame:CGRectZero andBg:nil andRadius:0 andAddSubView:self.contentView];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.contentView);
        make.height.mas_equalTo(YNUM(15));
    }];
}

- (UICollectionView *)myCollectionView {
    if (!_myCollectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake((SCREEN_W - YNUM(84))/4, YNUM(140));
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
//        layout.minimumLineSpacing = YNUM(13);
        layout.minimumInteritemSpacing = 0;
        UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        collectionView.dataSource = self;
        collectionView.delegate = self;
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.scrollEnabled = NO;
        collectionView.backgroundColor = [UIColor whiteColor];
        [collectionView registerClass:[AY_DetailTJCell class] forCellWithReuseIdentifier:@"AY_DetailTJCell"];
        _myCollectionView = collectionView;
    }
    return _myCollectionView;
}

- (NSArray *)sourceAry {
    if(!_sourceAry) {
        _sourceAry = [NSArray array];
    }
    return _sourceAry;
}

#pragma mark -- UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.sourceAry.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AY_DetailTJCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AY_DetailTJCell" forIndexPath:indexPath];
    if (self.sourceAry.count > indexPath.row) {
        cell.bookModel = self.sourceAry[indexPath.row];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookDetailViewController *bookDetailVC = [[AY_BookDetailViewController alloc] init];
    if(self.sourceAry.count > indexPath.row) {
        bookDetailVC.bookId = [self.sourceAry[indexPath.row] bookId];
    }
    [[AYTool viewControllerWith:self].navigationController pushViewController:bookDetailVC animated:YES];
}


- (void)setRecommendList:(NSArray *)recommendList {
    _recommendList = recommendList;
    self.sourceAry = [NSArray arrayWithArray:_recommendList];

    [self.myCollectionView reloadData];
}

- (void)setChangeBookListAction2 {
    if(self.recommendBlock) {
        self.recommendBlock();
    }
}

@end
